# 使用免费天气API
# 使用说明参考：https://www.sojson.com/api/weather.html

import requests,json
import re
import main

def getWeather(cityid):
    result_r = {'result':False};
    result = {}

    url = "http://t.weather.sojson.com/api/weather/city/"+str(cityid);
    r = requests.get(url)
    # print(r.text)
    weaterData = json.loads(r.text)
    
    try:
        if weaterData['status'] == 200 :
            # 昨天天气
            yesterday = weaterData['data']['yesterday']
        
            # 今天天气
            today = weaterData['data']['forecast'][0]
            result['todayType'] = today['type']
            result['todayLow'] = int(re.findall(r"-?\d+\.?\d*", today['low'])[0])
            result['todayHigh'] = int(re.findall(r"-?\d+\.?\d*", today['high'])[0])
            result['subYesterdayLow'] = int(result['todayLow']) - int(re.findall(r"-?\d+\.?\d*", yesterday['low'])[0])
            result['subYesterdayHigh'] = int(result['todayHigh']) - int(re.findall(r"-?\d+\.?\d*", yesterday['high'])[0])
            result_r['result'] = True
            result_r['data'] = result
            return result_r
        else:
            result_r['data'] = weaterData
            return result_r
    except Exception as e:
        result_r['data'] = e
        return result_r

# getWeather(101210106)

